var a = getApp(), t = require("../../A4730AC3A71FB5DFC21562C4223CA4D1.js");

Page({
    data: {
        page: 1,
        coupons: [],
        showExchange: !1,
        exchangeCode: "",
        fromPage: "my",
        total: 0,
        isLoading: !1,
        goodsIds: []
    },
    onLoad: function(a) {
        var t = this, o = {}, n = a.from, e = a.ids;
        if (e) {
            e = e.split(",");
            var i = {}, s = !0, d = !1, r = void 0;
            try {
                for (var c, h = e[Symbol.iterator](); !(s = (c = h.next()).done); s = !0) {
                    var u = c.value;
                    i[u] = u;
                }
            } catch (a) {
                d = !0, r = a;
            } finally {
                try {
                    !s && h.return && h.return();
                } finally {
                    if (d) throw r;
                }
            }
            o.goodsIds = e, o.goodsIdsObj = i;
        }
        n && (o.fromPage = n), this.setData(o, function() {
            t.loadCoupon(1, !0);
        });
    },
    onPullDownRefresh: function() {
        this.loadCoupon(1);
    },
    onReachBottom: function() {
        this.loadCoupon(this.data.page + 1);
    },
    tapNoUseCoupon: function() {
        a.globalData.chooseCouponItem = null, wx.navigateBack({
            delta: 1
        });
    },
    tapCoupon: function(t) {
        if ("order" === this.data.fromPage) {
            var o = t.currentTarget.dataset.id + "";
            if (1 === 1 * t.currentTarget.dataset.status) {
                var n = !0, e = !1, i = void 0;
                try {
                    for (var s, d = this.data.coupons[Symbol.iterator](); !(n = (s = d.next()).done); n = !0) {
                        var r = s.value;
                        if (r.coupon_id + "" === o && r.isCanUse) {
                            a.globalData.chooseCouponItem = r, wx.navigateBack({
                                delta: 1
                            });
                            break;
                        }
                    }
                } catch (a) {
                    e = !0, i = a;
                } finally {
                    try {
                        !n && d.return && d.return();
                    } finally {
                        if (e) throw i;
                    }
                }
            }
        }
    },
    loadCoupon: function(a) {
        var o = this, n = arguments.length > 1 && void 0 !== arguments[1] && arguments[1];
        this.data.isLoading || (this.setData({
            isLoading: !0
        }), n && wx.showLoading({
            mask: !0
        }), t.get("/v2/coupon/mine", {
            page: a
        }).then(function(t) {
            if (n && wx.hideLoading(), o.setData({
                isLoading: !1
            }), 1001 == t.data.code) {
                var e = o.data.coupons, i = t.data.response.data, s = o.data.goodsIds.length, d = o.data.goodsIdsObj;
                i = i.map(function(a) {
                    if (s) {
                        var t = !0, o = !1, n = void 0;
                        try {
                            for (var e, i = a.goods_id[Symbol.iterator](); !(t = (e = i.next()).done); t = !0) {
                                var r = e.value;
                                d[r.id] && (a.isCanUse = !0);
                            }
                        } catch (a) {
                            o = !0, n = a;
                        } finally {
                            try {
                                !t && i.return && i.return();
                            } finally {
                                if (o) throw n;
                            }
                        }
                    } else a.isCanUse = !0;
                    return a;
                }), e = 1 === a ? i : e.concat(i), o.setData({
                    coupons: e,
                    page: i.length > 0 ? a : a - 1,
                    total: t.data.response.total
                });
            }
        }).catch(function() {
            n && wx.hideLoading(), o.setData({
                isLoading: !1
            });
        }));
    },
    tapOpenExchangeButton: function() {
        this.setData({
            showExchange: !0
        });
    },
    tapDoExchangeButton: function() {
        var a = this;
        this.data.exchangeCode.trim().length < 1 ? wx.showToast({
            title: "请输入兑换码",
            icon: "none",
            duration: 1e3,
            mask: !0
        }) : (wx.showLoading(), t.post("/v2/coupon/exchange", {
            coupon_code: this.data.exchangeCode
        }).then(function(t) {
            wx.hideLoading(), 1001 === t.data.code && (a.setData({
                showExchange: !1,
                exchangeCode: null
            }), a.loadCoupon(1));
        }).catch(function() {
            wx.hideLoading();
        }));
    },
    tapCloseExchangeButton: function() {
        this.setData({
            showExchange: !1
        });
    },
    bindInputExchangeCode: function(a) {
        this.setData({
            exchangeCode: a.detail.value
        });
    }
});